﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using PortfolioApp.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace PortfolioApp.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool seedUsers = false, bool seedPosts = true)
        {
            var user01 = new ApplicationUser()
            {
                UserName = "SuperAdmin",
                Email = "superadmin@mail.com"
            };

            if (seedUsers)
            {


                RoleManager<IdentityRole> rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
                if (!rm.RoleExists("Admin"))
                {
                    rm.Create(new IdentityRole("Admin"));
                }
                db.SaveChanges();


                UserManager<ApplicationUser> um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

                if (db.Users.Where(x => x.UserName == user01.UserName).FirstOrDefault() == null)
                {
                    var result = um.Create(user01, "pass123");
                    db.SaveChanges();
                    if (result.Succeeded)
                    {
                        um.AddToRole(user01.UserName, "Admin");
                        db.SaveChanges();
                    }
                }
            }

            if (seedPosts)
            {
                var user = db.Users.Where(x => x.UserName == user01.UserName).FirstOrDefault();

                db.Posts.AddOrUpdate(
                    x => x.Comment,
                    new Post(user.Id, "First Post", "This is my first post."),
                    new Post(user.Id, "Second Post", "This is my 2nd post."),
                    new Post(user.Id, "Third Post", "This is my 3rd post."),
                    new Post(user.Id, "Fourth Post", "This is my 4th post."),
                    new Post(user.Id, "Fifth Post", "This is my 5th post."),
                    new Post(user.Id, "Sixth Post", "This is my 6th post."),
                    new Post(user.Id, "Seventh Post", "This is my 7thpost."),
                    new Post(user.Id, "Eighth Post", "This is my 8th post."),
                    new Post(user.Id, "Ninth Post", "This is my 9th post."),
                    new Post(user.Id, "Tenth Post", "This is my 10th post."),
                    new Post(user.Id, "Post 11", "This is my 11th post."),
                    new Post(user.Id, "Post 12", "This is my 12th post."),
                    new Post(user.Id, "Post 13", "This is my 13th post."),
                    new Post(user.Id, "Post 14", "This is my 14th post.")
                    );
                db.SaveChanges();

                db.Projects.AddOrUpdate(
                    x => x.Title,
                    new Project(user.Id, "First Project", "This is my first project.", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg"),
                    new Project(user.Id, "Second Project", "This is my second project.", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg"),
                    new Project(user.Id, "Third Project", "This is my third project.", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg"),
                    new Project(user.Id, "Fourth Project", "This is my fourth project.", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg"),
                    new Project(user.Id, "Fifth Project", "This is my fifth project.", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg"),
                    new Project(user.Id, "Sixth Project", "This is my sixth project.", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg", "http://www.solplus.co.uk/wp-content/uploads/2013/03/projectManagement.jpg")
                    );
                db.SaveChanges();
            }

        }
    }
}
